<?php
namespace Modules\Chip\Http\Controllers\Admin\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\Chip\Entities\Chip;

use Validator;
use ThrowException;
use DB;

class ChipController extends Controller
{
    // 列表
    public function index(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $options = $req->all();

        $list = Chip::getList($options);

        return $list->generateListResponse();
    }

    // 创建套餐
    public function store(Request $req)
    {
        $info = $req->all();

        // if (!@$info['price']) {
        //     ThrowException::Conflict('价格不能为0');
        // }

        $item = Chip::create($info);

        return [
            'id' => $item->id,
            'uuid' => $item->uuid
        ];
    }

    // 套餐详情
    public function show(Request $req, $uuid)
    {
        $item = Chip::getEntity([
            'uuid' => $uuid
        ]);

        $info = $item->getInfo();

        return [
            'info' => $info
        ];
    }

    // 更新套餐信息
    public function update(Request $req, $uuid)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $item = Chip::getEntity([
            'uuid' => $uuid
        ]);

        DB::beginTransaction();

        if ($req->type === 'update') {
            $info = $req->input('attributes');
            $item->update($info);
        }

        DB::commit();

        return [];
    }

    // 删除套餐
    public function destroy(Request $req, $uuid)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $item = Chip::getEntity([
            'uuid' => $uuid
        ]);

        $item->delete();

        return [];
    }
}

